package com.peanut.baby.util;

import com.peanut.devlibrary.util.StringUtil;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by anonymouslystudio on 2017/7/25.
 */

public class TimeUtil {
    public static String getDisplayTime(String dateString){
        if(StringUtil.isNullOrEmpty(dateString)){
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String timestamp = dateString;
        Calendar today = Calendar.getInstance();
        Calendar target = Calendar.getInstance();

        DateFormat mh = new SimpleDateFormat("HH:mm");
        try {
            Date date = format.parse(dateString);
            today.setTime(new Date(System.currentTimeMillis()));
            today.set(Calendar.HOUR, 0);
            today.set(Calendar.MINUTE, 0);
            today.set(Calendar.SECOND, 0);
            target.setTime(date);
            target.set(Calendar.HOUR, 0);
            target.set(Calendar.MINUTE, 0);
            target.set(Calendar.SECOND, 0);

            int xcts = 9999999;
            if (today.get(Calendar.YEAR) == target.get(Calendar.YEAR)
                    && today.get(Calendar.MONTH) == target.get(Calendar.MONTH)) {
                xcts = target.get(Calendar.DAY_OF_MONTH) - today.get(Calendar.DAY_OF_MONTH);
            }

            switch ((int)xcts) {
                case 0:
                    timestamp = "今天"+mh.format(date);
                    break;
                case -1:
                    timestamp = "昨天"+mh.format(date);
                    break;
                case -2:
                    timestamp = "前天"+mh.format(date);;
                    break;
                case 1:
                    timestamp = "明天"+mh.format(date);
                    break;
                case 2:
                    timestamp = "后天"+mh.format(date);;
                    break;
                default:
                    timestamp = format2.format(date);
                    break;
            }
            return timestamp;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return timestamp;

    }

    public static String getQAListDisplayTime(long timeMillis){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String timestamp = "";
        Calendar today = Calendar.getInstance();
        Calendar target = Calendar.getInstance();

        DateFormat mh = new SimpleDateFormat("HH:mm");

        Date date = new Date(timeMillis);
        today.setTime(new Date(System.currentTimeMillis()));
        today.set(Calendar.HOUR, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        target.setTime(date);
        target.set(Calendar.HOUR, 0);
        target.set(Calendar.MINUTE, 0);
        target.set(Calendar.SECOND, 0);

        int xcts = 9999999;
        if (today.get(Calendar.YEAR) == target.get(Calendar.YEAR)
                && today.get(Calendar.MONTH) == target.get(Calendar.MONTH)) {
            xcts = target.get(Calendar.DAY_OF_MONTH) - today.get(Calendar.DAY_OF_MONTH);
        }

        switch ((int)xcts) {
            case 0:
                timestamp = "今天"+mh.format(date);
                break;
            case -1:
                timestamp = "昨天"+mh.format(date);
                break;
            case -2:
                timestamp = "前天"+mh.format(date);;
                break;
            default:
                timestamp = format.format(date);
                break;
        }
        return timestamp;


    }

    public static String getDisplayTimeMillis(long timeMillis){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timestamp = "";
        Calendar today = Calendar.getInstance();
        Calendar target = Calendar.getInstance();

        DateFormat mh = new SimpleDateFormat("HH:mm");

        Date date = new Date(timeMillis);
        today.setTime(new Date(System.currentTimeMillis()));
        today.set(Calendar.HOUR, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        target.setTime(date);
        target.set(Calendar.HOUR, 0);
        target.set(Calendar.MINUTE, 0);
        target.set(Calendar.SECOND, 0);

        int xcts = 9999999;
        if (today.get(Calendar.YEAR) == target.get(Calendar.YEAR)
                && today.get(Calendar.MONTH) == target.get(Calendar.MONTH)) {
            xcts = target.get(Calendar.DAY_OF_MONTH) - today.get(Calendar.DAY_OF_MONTH);
        }

        switch ((int)xcts) {
            case 0:
                timestamp = "今天"+mh.format(date);
                break;
            case -1:
                timestamp = "昨天"+mh.format(date);
                break;
            case -2:
                timestamp = "前天"+mh.format(date);;
                break;
            default:
                timestamp = format.format(date);
                break;
        }
        return timestamp;


    }

    public static String getWeekString(String dateStr){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = format.parse(dateStr);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int week = calendar.get(Calendar.DAY_OF_WEEK);
            return getWeekString(week);
        } catch (ParseException e) {
            return "";
        }

    }

    public static String getWeekString(int dayOfWeek) {
        String result = "";
        switch (dayOfWeek) {
            case Calendar.MONDAY:
                result = "周一";
                break;
            case Calendar.TUESDAY:
                result = "周二";
                break;
            case Calendar.WEDNESDAY:
                result = "周三";
                break;
            case Calendar.THURSDAY:
                result = "周四";
                break;
            case Calendar.FRIDAY:
                result = "周五";
                break;
            case Calendar.SATURDAY:
                result = "周六";
                break;
            case Calendar.SUNDAY:
                result = "周日";
                break;
        }
        return result;

    }

    public static boolean isCloseEnough(long var0, long var2) {
        long var4 = var0 - var2;
        if(var4 < 0L) {
            var4 = -var4;
        }

        return var4 < 30000L;
    }
}
